package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCatMapper;
import com.sky.service.ShoppingCatService;
import com.sky.utils.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCatServiceImpl implements ShoppingCatService
{

    @Autowired
    private ShoppingCatMapper shoppingCatMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCat(ShoppingCartDTO shoppingCartDTO)
    {
        //通过商品 用户ID 查询购物车表，判断当前商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        Long userId = BaseContext.getCurrentId();//获取当前用户的ID
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list = shoppingCatMapper.list(shoppingCart);

        //如果存在，修改购物车，将对应商品数量加1
        if(list != null && list.size() > 0)
        {
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);

            shoppingCatMapper.updateNumberById(cart);
        }
        else
        {
            //如果不存在，插入一条购物策车数据
            Long dishId = shoppingCartDTO.getDishId();
            //判断本次添加到购物车的商品是套餐还是菜品
            if(dishId != null)
            {
                //本次添加到购物车的是菜品
                Dish dish = dishMapper.getById(dishId);

                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            } else {
                //本次添加到购物车的是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);

                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCatMapper.insert(shoppingCart);
        }


    }

    @Override
    public List<ShoppingCart> showShoppingCart()
    {
        //获取当前用户ID
        Long currentUserId = BaseContext.getCurrentId();

        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(currentUserId)
                .build();

        List<ShoppingCart> list = shoppingCatMapper.list(shoppingCart);

        return list;
    }


    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart()
    {
        //获取当前用户ID
        Long currentUserId = BaseContext.getCurrentId();

        shoppingCatMapper.deleteByUserId(currentUserId);
    }


    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subshoppingCat(ShoppingCartDTO shoppingCartDTO)
    {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //查询当前用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCatMapper.list(shoppingCart);

        if(list != null && list.size() > 0)
        {
            shoppingCart = list.get(0);

            //当前需要删除的商品
            if(shoppingCart.getNumber() == 1)
            {
                //份数为一，直接删除当前记录
                shoppingCatMapper.deleteById(shoppingCart.getId());
            }
            else {
                //否则修改当前商品的份数
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCatMapper.updateNumberById(shoppingCart);
            }
        }


    }
}
